﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.SessionState;
using OLTS.DATA_SERVICE;
using OLTS.MODEL;
using OLTS.MODEL.Custom_Model;
using OLTS.WEB.BaseClass;


namespace ONTS.WEB.Service
{
    /// <summary>
    /// Summary description for WorkDetailsServices
    /// </summary>
    public class WorkDetailsServices : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            var currentuser = BaseService.LOGIN.EMPLOYEE_ID;
            var date = request.QueryString["date"].ToString();
            var stime = request.QueryString["stime"].ToString();
            var etime = request.QueryString["etime"].ToString();
            var leave = request.QueryString["leave"].ToString();
            var cmd = new TimeSheet_Service();
            var listDB = cmd.GetTop5ByCurrentUserandDate(currentuser);
            // DataSource.Clear();
            foreach (var parent in listDB)
            {
                var obj = DataSource.Where(x => x.ID == parent.ID).FirstOrDefault();
                if (obj == null)
                {
                    DataSource.Add(parent);
                }

            }

            if (request["oper"] == "add")
            {
                var childobj = new OLTS.MODEL.OTS_WORK_DETAIL();
                childobj.AMOUNT_OF_TIME = Utility.Utility.checkInt(request["AMOUNT_OF_TIME"]);
                childobj.AMOUNT_OF_TIME_OVER_TIME = Utility.Utility.checkInt(request["AMOUNT_OF_TIME_OVER_TIME"]);
                childobj.DETAIL = request["DETAIL"];
                childobj.CREATED = DateTime.Now;
                childobj.CREATE_BY = currentuser;
                childobj.Action = OLTS.MODEL.ActionEnum.Create;
                var parent = DataSource.Where(x => x.EMPLOYEE_ID == currentuser && x.DATE == Convert.ToDateTime(date)).FirstOrDefault();
                if (parent != null)
                {
                    var childlist = parent.OTS_WORK_DETAIL;
                    childlist.Add(childobj);
                }
                else
                {
                    var holiday = isHoliday();
                    var childList = new List<OLTS.MODEL.OTS_WORK_DETAIL>();
                    parent = new OLTS.MODEL.OTS_TIME_SHEET();
                    parent.DATE = Convert.ToDateTime(date);
                    parent.TIME = stime + "-" + etime;
                    parent.EMPLOYEE_ID = currentuser;
                    if (holiday != null)
                    {
                        parent.HOLIDAY_ID = holiday.ID;
                    }
                    if (leave != "0")
                    {
                        parent.LEAVE_ID = Utility.Utility.checkInt(leave);
                    }
                    parent.CREATE_BY = currentuser;
                    parent.CREATED = DateTime.Now;
                    childList.Add(childobj);
                    parent.OTS_WORK_DETAIL = childList;
                    parent.Action = ActionEnum.Create;
                    DataSource.Add(parent);
                }
            }
            else
            {
                if (request["oper"] == "del")
                {
                    var parent = DataSource.Where(x => x.EMPLOYEE_ID == currentuser && x.DATE == Convert.ToDateTime(date)).FirstOrDefault();
                    if (parent != null)
                    {
                        var childlist = parent.OTS_WORK_DETAIL;
                        var id = Convert.ToInt32(request["ID"]);
                        var obj = childlist.Where(x => x.ID == id).FirstOrDefault();
                        if (obj != null)
                        {
                            obj.Action = ActionEnum.Delete;
                        }
                    }
                }
                else if (request["oper"] == "edit")
                {
                    var parent = DataSource.Where(x => x.EMPLOYEE_ID == currentuser && x.DATE == Convert.ToDateTime(date)).FirstOrDefault();
                    if (parent != null)
                    {
                        var childlist = parent.OTS_WORK_DETAIL;
                        var id = Convert.ToInt32(request["ID"]);
                        var obj = childlist.Where(x => x.ID == id).FirstOrDefault();
                        if (obj != null)
                        {
                            obj.AMOUNT_OF_TIME = Utility.Utility.checkInt(request["AMOUNT_OF_TIME"]);
                            obj.AMOUNT_OF_TIME_OVER_TIME = Utility.Utility.checkInt(request["AMOUNT_OF_TIME_OVER_TIME"]);
                            obj.DETAIL = request["DETAIL"];
                            obj.CREATED = DateTime.Now;
                            obj.CREATE_BY = currentuser;
                            obj.Action = ActionEnum.Update;
                            parent.Action = ActionEnum.Update;
                        }
                    }
                }
            }


            var pObj = DataSource.Where(x => x.DATE == Convert.ToDateTime(date) && x.EMPLOYEE_ID == currentuser).FirstOrDefault();
            var resultList = new List<OLTS.MODEL.Custom_Model.OTS_WORK_DETAIL>();
            if (pObj != null)
            {
                var row = 0;
                var chiList = pObj.OTS_WORK_DETAIL;
                foreach (var item in chiList)
                {
                    if (item.Action != ActionEnum.Delete)
                    {
                        row = row + 1;
                        var obj = new OLTS.MODEL.Custom_Model.OTS_WORK_DETAIL();
                        obj.ID = item.ID;
                        obj.SEQ = row;
                        obj.RowIndex = row;
                        obj.AMOUNT_OF_TIME = item.AMOUNT_OF_TIME;
                        obj.AMOUNT_OF_TIME_OVER_TIME = item.AMOUNT_OF_TIME_OVER_TIME;
                        obj.DETAIL = item.DETAIL;
                        resultList.Add(obj);
                    }
                }
            }

            int total = resultList.Count == 0 ? 1 : (int)Math.Ceiling((double)resultList.Count / 10);

            var jsonData = new
            {
                total = total,
                page = request["page"],
                records = resultList.Count,
                rows = resultList.Skip(Convert.ToInt16(request["page"]) * 10 - 10).Take(10)
            };

            response.Write(new JavaScriptSerializer().Serialize(jsonData));
        }

        public List<OLTS.MODEL.OTS_TIME_SHEET> DataSource
        {
            get
            {
                var d = (List<OLTS.MODEL.OTS_TIME_SHEET>)HttpContext.Current.Session["detail"];
                if (d == null)
                {
                    d = new List<OLTS.MODEL.OTS_TIME_SHEET>();
                    HttpContext.Current.Session["detail"] = d;
                }
                return d;
            }
            set
            {
                HttpContext.Current.Session["detail"] = value;
            }
        }

        private OTS_HOLIDAY isHoliday()
        {

            var currentuser = BaseService.LOGIN.EMPLOYEE_ID;
            var site = BaseService.PROJECT.SITE_ID;
            var yr = DateTime.Now.Year.ToString();
            var cmd = new Holiday_Service(yr, Convert.ToInt16(site));
            var result = cmd.GetListHoliday();
            var isholiday = result.Where(x => x.DATE == DateTime.Now.Date).FirstOrDefault();
            if (isholiday != null)
            {
                return isholiday;
            }
            else
            {
                return null;
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}